package com.ebelter.seneo.ui.fragments.main;


import android.app.Dialog;
import android.os.Bundle;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.bluetooth.products.scale.bean.ScaleMeasureResult;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserCurrentMeasureRecord;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoBTManager;

import com.ebelter.seneo.models.SeneoDbUtils;
import com.ebelter.seneo.models.UploadData2ServiceUtil;
import com.ebelter.seneo.models.googlefit.GoogleFitManager;
import com.ebelter.seneo.ui.activitys.GuestSelectActivity;
import com.ebelter.seneo.ui.activitys.SeneoMainActivity;
import com.ebelter.seneo.ui.fragments.BaseFragment;
import com.ebelter.seneo.ui.views.OfflineTextTipDialog;
import com.ebelter.seneo.ui.views.WeighTipDialog;
import com.ebelter.seneo.utils.BodyCompositionCriteria;
import com.ebelter.seneo.utils.SeneoUtils;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


//填写密码
public class Dash_Fragment extends BaseFragment {


    public static final String TAG = "Dash_Fragment";
    public static final String Non = "--";


    ImageView dash_topsex_iv;//性别图标
    TextView dash_topname_tv;//名字
    TextView dash_toptime_tv;//顶部显示的时间
    ImageView dash_topconnect_iv;//顶部的连接状态

    TextView dash_weight_biaozhun_tv;//体重旁边的标准视图
    TextView dash_weight_value_tv;
    ImageView dash_changeWeight_iv;
    TextView dash_changeWeightValue_tv;
    TextView dash_weight_unit_tv;

    ImageView dash_changefat_iv;
    TextView dash_changefatValue_tv;
    TextView dash_changeWeightValueUnit_tv;

    ScrollView dash_sv;//测量结果整体视图
    LinearLayout dash_tip_ll;//未测量提示视图


    TextView dash_item_weight_tv;//体重
    TextView dash_item_weight2_tv;

    TextView dash_measure_time_tv;

    TextView dash_item_ToFatWeight_tv;
    TextView dash_item_ToFatWeight2_tv;

    TextView dash_item_WeightControl_tv;
    TextView dash_item_WeightControl2_tv;

    TextView dash_item_Bmi_tv;
    TextView dash_item_Bmi2_tv;

    TextView dash_item_bodyfatPercentage_tv;
    TextView dash_item_bodyfatPercentage2_tv;

    TextView dash_item_Bmr_tv;
    TextView dash_item_Bmr2_tv;

    TextView dash_item_visfat_tv;
    TextView dash_item_visfat2_tv;

    TextView dash_item_muscle_tv;
    TextView dash_item_muscle2_tv;

    TextView dash_item_bone_tv;
    TextView dash_item_bone2_tv;

    TextView dash_item_protein_tv;
    TextView dash_item_protein2_tv;

    TextView dash_item_water_tv;
    TextView dash_item_water2_tv;

    TextView dash_item_skeletal_tv;
    TextView dash_item_skeletal2_tv;

    TextView dash_item_bodyscore_tv;
    TextView dash_item_bodyscore2_tv;

    TextView dash_item_bodyage_tv;
    TextView dash_item_bodyage2_tv;

    TextView dash_item_size_tv;

    ImageView dash_item_weight_iv;
    ImageView dash_item_ToFatWeight_iv;
    ImageView dash_item_WeightControl_iv;
    ImageView dash_item_Bmi_iv;
    ImageView dash_item_bodyfatPercentage_iv;
    ImageView dash_item_Bmr_iv;
    ImageView dash_item_visfat_iv;
    ImageView dash_item_muscle_iv;
    ImageView dash_item_bone_iv;
    ImageView dash_item_protein_iv;
    ImageView dash_item_water_iv;
    ImageView dash_item_skeletal_iv;
    ImageView dash_item_bodyscore_iv;
    ImageView dash_item_bodyage_iv;
    ImageView dash_item_size_iv;


    //private WeighTipDialog mWeighTipDialog;//超重2kg提示框

    private void FV() {
        dash_topsex_iv = findViewById(R.id.dash_topsex_iv);
        dash_topname_tv = findViewById(R.id.dash_topname_tv);
        dash_toptime_tv = findViewById(R.id.dash_toptime_tv);
        dash_topconnect_iv = findViewById(R.id.dash_topconnect_iv);
        dash_weight_biaozhun_tv = findViewById(R.id.dash_weight_biaozhun_tv);
        dash_sv = findViewById(R.id.dash_sv);
        dash_tip_ll = findViewById(R.id.dash_tip_ll);
        dash_item_weight_tv = findViewById(R.id.dash_item_weight_tv);
        dash_item_weight2_tv = findViewById(R.id.dash_item_weight2_tv);
        dash_item_ToFatWeight_tv = findViewById(R.id.dash_item_ToFatWeight_tv);
        dash_item_ToFatWeight2_tv = findViewById(R.id.dash_item_ToFatWeight2_tv);
        dash_item_WeightControl_tv = findViewById(R.id.dash_item_WeightControl_tv);
        dash_item_WeightControl2_tv = findViewById(R.id.dash_item_WeightControl2_tv);
        dash_item_Bmi_tv = findViewById(R.id.dash_item_Bmi_tv);
        dash_item_Bmi2_tv = findViewById(R.id.dash_item_Bmi2_tv);
        dash_item_bodyfatPercentage_tv = findViewById(R.id.dash_item_bodyfatPercentage_tv);
        dash_item_bodyfatPercentage2_tv = findViewById(R.id.dash_item_bodyfatPercentage2_tv);
        dash_item_Bmr_tv = findViewById(R.id.dash_item_Bmr_tv);
        dash_item_Bmr2_tv = findViewById(R.id.dash_item_Bmr2_tv);
        dash_item_visfat_tv = findViewById(R.id.dash_item_visfat_tv);
        dash_item_visfat2_tv = findViewById(R.id.dash_item_visfat2_tv);
        dash_item_muscle_tv = findViewById(R.id.dash_item_muscle_tv);
        dash_item_muscle2_tv = findViewById(R.id.dash_item_muscle2_tv);
        dash_item_bone_tv = findViewById(R.id.dash_item_bone_tv);
        dash_item_bone2_tv = findViewById(R.id.dash_item_bone2_tv);
        dash_item_protein_tv = findViewById(R.id.dash_item_protein_tv);
        dash_item_protein2_tv = findViewById(R.id.dash_item_protein2_tv);
        dash_item_water_tv = findViewById(R.id.dash_item_water_tv);
        dash_item_water2_tv = findViewById(R.id.dash_item_water2_tv);
        dash_item_skeletal_tv = findViewById(R.id.dash_item_skeletal_tv);
        dash_item_skeletal2_tv = findViewById(R.id.dash_item_skeletal2_tv);
        dash_item_bodyscore_tv = findViewById(R.id.dash_item_bodyscore_tv);
        dash_item_bodyscore2_tv = findViewById(R.id.dash_item_bodyscore2_tv);
        dash_item_bodyage_tv = findViewById(R.id.dash_item_bodyage_tv);
        dash_item_bodyage2_tv = findViewById(R.id.dash_item_bodyage2_tv);
        dash_item_size_tv = findViewById(R.id.dash_item_size_tv);
        dash_measure_time_tv = findViewById(R.id.dash_measure_time_tv);
        dash_weight_unit_tv = findViewById(R.id.dash_weight_unit_tv);
        dash_changeWeightValueUnit_tv = findViewById(R.id.dash_changeWeightValueUnit_tv);
        dash_weight_value_tv = findViewById(R.id.dash_weight_value_tv);
        dash_changeWeight_iv = findViewById(R.id.dash_changeWeight_iv);
        dash_changefat_iv = findViewById(R.id.dash_changefat_iv);
        dash_changeWeightValue_tv = findViewById(R.id.dash_changeWeightValue_tv);
        dash_changefatValue_tv = findViewById(R.id.dash_changefatValue_tv);

        dash_item_weight_iv = findViewById(R.id.dash_item_weight_iv);
        dash_item_ToFatWeight_iv = findViewById(R.id.dash_item_ToFatWeight_iv);
        dash_item_WeightControl_iv = findViewById(R.id.dash_item_WeightControl_iv);
        dash_item_Bmi_iv = findViewById(R.id.dash_item_Bmi_iv);
        dash_item_bodyfatPercentage_iv = findViewById(R.id.dash_item_bodyfatPercentage_iv);
        dash_item_Bmr_iv = findViewById(R.id.dash_item_Bmr_iv);
        dash_item_visfat_iv = findViewById(R.id.dash_item_visfat_iv);
        dash_item_muscle_iv = findViewById(R.id.dash_item_muscle_iv);
        dash_item_bone_iv = findViewById(R.id.dash_item_bone_iv);
        dash_item_protein_iv = findViewById(R.id.dash_item_protein_iv);
        dash_item_water_iv = findViewById(R.id.dash_item_water_iv);
        dash_item_skeletal_iv = findViewById(R.id.dash_item_skeletal_iv);
        dash_item_bodyscore_iv = findViewById(R.id.dash_item_bodyscore_iv);
        dash_item_bodyage_iv = findViewById(R.id.dash_item_bodyage_iv);
        dash_item_size_iv = findViewById(R.id.dash_item_size_iv);

    }


    @Override
    protected void setListener() {
        dash_topsex_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getActivity() != null) {
                    ((SeneoMainActivity) getActivity()).mainStartActivityForResult(GuestSelectActivity.TAG);
                }
            }
        });
    }


    @Override
    protected int setLayoutResouceId() {
        return R.layout.m_dash_fly;
    }

    @Override
    protected void initView() {
        FV();
    }


    public void setConnectFlagIV(boolean isConnected) {
        if (isConnected) {
            ViewUtils.displayView(dash_topconnect_iv);
        } else {
            ViewUtils.hideView(dash_topconnect_iv);
        }
    }

    private UploadData2ServiceUtil mUploadData2ServiceUtil;//上传工具

    @Override
    protected void initData(Bundle arguments) {

        EventBus.getDefault().register(this);

        mUploadData2ServiceUtil = new UploadData2ServiceUtil();//上传工具类

        updateDateTime();//01显示日期时间

        updateSexNick();//02显示用户名和性别

        updateUnit();//更新单位

    }

    //更新单位
    private void updateUnit() {
        String unitStr = (SeneoUtils.getUnitModel() == 0 ? IUserContant.IDIS.kg : IUserContant.IDIS.lb);
        //设置单位
        ViewUtils.setTextViewStr(dash_weight_unit_tv, unitStr);
        ViewUtils.setTextViewStr(dash_changeWeightValueUnit_tv, unitStr);
    }

    public void updateSexNick() {
        if ((getActivity()) != null) {
            SeneoBTManager seneoBTManager = ((SeneoMainActivity) getActivity()).getSeneoBTManager();
            if (seneoBTManager != null) {
                SeneoUserinfos currentUserinfos = seneoBTManager.getCurrentUserinfos();
                if (currentUserinfos != null) {
                    setDisplayUserInfo(currentUserinfos.sex, currentUserinfos.nickName);
                } else {
                    LogUtils.i(TAG, "---updateSexNick-找不到用户信息");
                }
            }
        }
    }


    //更新显示的时间
    public void updateDateTime() {
        //ViewUtils.setTextViewStr(dash_toptime_tv, TimeUtils.timeFatmat(TimeUtils.getCurrentTime1(),1));
        ViewUtils.setTextViewStr(dash_toptime_tv, TimeUtils.formatDate_nyr(CommonLib.APPContext, System.currentTimeMillis()));
    }

    public void setDisplayUserInfo(int sex, String nickName) {
        ViewUtils.setImageResource(dash_topsex_iv, sex == 0 ? R.drawable.icon_male_main : R.drawable.icon_female_main);
        ViewUtils.setTextViewStr(dash_topname_tv, nickName);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, TAG + "--onEventMainThread接收到CommonEventBus信息--" + event.toString());
        if (event.cmd == IUserContant.ICMDS.UPDATE_CURRENT_USER_INFO) {
            LogUtils.i(TAG, TAG + "--onEventMainThread--更新当前用户信息");
            updateSexNick();//更新性别框
        } else if (event.cmd == IUserContant.ICMDS.UPDATE_UNIT_DISPLAY) {
            LogUtils.i(TAG, TAG + "--onEventMainThread--切换单位");
            displayLastMeasureData();
        } else if (event.cmd == IUserContant.ICMDS.DISPLAY_DASH_USER_DATA) {
            LogUtils.i(TAG, TAG + "--onEventMainThread---显示最后用户的测量信息");
            displaySeneoUserCurrentMeasureRecordData();
        } else if (event.cmd == IUserContant.ICMDS.DISPLAY_DASH_USER_DATA_NULL) {
            LogUtils.i(TAG, TAG + "--onEventMainThread--显示当前页面为空数据");
            displayNullMeasure();
        }
    }

    private void displayNullMeasure() {
        ViewUtils.goneView(dash_sv);
        ViewUtils.displayView(dash_tip_ll);
        diaplayAllNon();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    //接收到测量结果 01
    public void onReceiveMeasureResult(ScaleMeasureResult scaleMeasureResult) {
        if ((getActivity()) != null) {
            SeneoBTManager seneoBTManager = ((SeneoMainActivity) getActivity()).getSeneoBTManager();
            if (seneoBTManager != null) {
                SeneoUserinfos currentUserinfos = seneoBTManager.getCurrentUserinfos();
                if (currentUserinfos != null) {
                    dealOnReceiveMeasureResult(currentUserinfos, scaleMeasureResult);
                } else {
                    LogUtils.i(TAG, "---onReceiveMeasureResult-找不到用户信息-A");
                }
            }
        }
    }

    //处理测量数据 02
    private void dealOnReceiveMeasureResult(final SeneoUserinfos currentUserinfos, final ScaleMeasureResult scaleMeasureResult) {
        float currentWeight = currentUserinfos.currentWeight;

        if (scaleMeasureResult.getAge() < 18) {
            ToastUtil.show(R.string.select_age_tip);//未满18岁测脂错误提示
        }


        //处理如果测量的值跟现在的用户测量误差有2kg以上的要弹框提示
        if (currentWeight != 0 && Math.abs(currentWeight - scaleMeasureResult.weight) > 2) {

//            if (mWeighTipDialog != null) {
//                mWeighTipDialog.cancel();
//                mWeighTipDialog = null;
//            }
            WeighTipDialog mWeighTipDialog = new WeighTipDialog(getActivity());
            mWeighTipDialog.setListener(new WeighTipDialog.ICancelOkBtListener() {
                @Override
                public void cancel(Dialog dialogView) {
                    dialogView.cancel();

                    displaySeneoUserCurrentMeasureRecordData();//取消以后显示最后一次测量的数据

                }

                @Override
                public void ok(Dialog dialogView) {
                    dialogView.cancel();
                    ToastUtil.show("Upload successfully");//上传成功提示语
                    upLoadAndDisplay(currentUserinfos, scaleMeasureResult);
                }
            });
            mWeighTipDialog.show();

        } else {
            ToastUtil.show("Upload successfully");//上传成功提示语
//            if (mWeighTipDialog != null) {
//                mWeighTipDialog.cancel();
//                mWeighTipDialog = null;
//            }
            upLoadAndDisplay(currentUserinfos, scaleMeasureResult);
        }


    }

    //接收到测量结果 03
    private void upLoadAndDisplay(SeneoUserinfos currentUserinfos, ScaleMeasureResult scaleMeasureResult) {


        if (scaleMeasureResult!=null){

            //确认好这个体重没问题 准备重新下发一次用户信息

//            if (getActivity()!=null){
//                ((SeneoMainActivity)getActivity()).restartSendUserInfo((int) scaleMeasureResult.getResistance(),scaleMeasureResult.weight);
//            }



            if (scaleMeasureResult.weight>0&&scaleMeasureResult.getAge()>=18&&scaleMeasureResult.fat<=0){
                if (SeneoMainActivity.isResum){
                    LogUtils.i(TAG,"执行了未检测到脂肪率的错误Fat measurement error. Please stand uprightly on the scale barefoot and try again.");
                    ToastUtil.show2("Fat measurement error. Please stand uprightly on the scale barefoot and try again.");
                }

            }
        }


        displayMeasureData(currentUserinfos, scaleMeasureResult);

        if (mUploadData2ServiceUtil != null) {
            mUploadData2ServiceUtil.uploadData(TAG, currentUserinfos, scaleMeasureResult);
        }
    }


    private ScaleMeasureResult lastDisScaleMeasureResult;//最后显示的测量数据 单位要跟手机系统的了
    private SeneoUserinfos lastDisSeneoUserinfos;//最后显示的测量数据 单位要跟手机系统的了
    private float changeWeightKg;
    private float changeFat;

    //显示测量ing的数据
    private void displayMeasureData(SeneoUserinfos currentUserinfos, ScaleMeasureResult scaleMeasureResult) {
        //记录测量过来的单位
        int unit = SeneoUtils.getUnitModel();
        if (IUserContant.IDIS.kg.equals(scaleMeasureResult.weightUnit) && unit != 0) {
            SeneoUtils.setUnitModel(0);//kg模式下
            SeneoUtils.sendCommonEventBus(TAG, Set_Fragment.TAG + Dash_Fragment.TAG + Trend_Fragment.TAG, IUserContant.ICMDS.UPDATE_UNIT_DISPLAY, "更新单位显示");
        } else if (IUserContant.IDIS.lb.equals(scaleMeasureResult.weightUnit) && unit != 1) {
            SeneoUtils.setUnitModel(1);
            SeneoUtils.sendCommonEventBus(TAG, Set_Fragment.TAG + Dash_Fragment.TAG + Trend_Fragment.TAG, IUserContant.ICMDS.UPDATE_UNIT_DISPLAY, "更新单位显示");
        }


        //记录改变的值 这个值永远是kg来做单位的
        SeneoUserCurrentMeasureRecord CMR = SeneoDbUtils.getSeneoUserCurrentMeasureRecord(currentUserinfos.host_id, currentUserinfos.user_id);
        if (CMR != null) {
            //说明有上一次记录的值了
            if (CMR.weight != 0) {//上一次有体重记录值
                changeWeightKg = scaleMeasureResult.getWeight() - CMR.weight;
            } else {//上一次没有体重记录值
                changeWeightKg = 0;
            }
            if (CMR.fat != 0) {//上一次有体脂记录值
                changeFat = scaleMeasureResult.getFat() - CMR.fat;
            } else {//上一次没有体脂记录值
                changeFat = 0;
            }
        } else { //说明没有上一次记录的值了
            changeWeightKg = 0;
            changeFat = 0;

        }
        lastDisScaleMeasureResult = scaleMeasureResult;
        lastDisSeneoUserinfos = currentUserinfos;

        //保存当前用户数据到最新的记录表中
        float weigh = lastDisScaleMeasureResult.weight;
        float bmi = lastDisScaleMeasureResult.bmi;
        float fat = lastDisScaleMeasureResult.fat;
        float bmr = lastDisScaleMeasureResult.bmr;
        float vf = lastDisScaleMeasureResult.visceralFat;
        float muscle = lastDisScaleMeasureResult.muscleVolume;
        float bone = lastDisScaleMeasureResult.boneVolume;
        float protein = lastDisScaleMeasureResult.protein;
        float water = lastDisScaleMeasureResult.waterRate;
        float skeletal = lastDisScaleMeasureResult.skeletalMuscle;
        float bodyScore = lastDisScaleMeasureResult.bodyScore;
        float bodyAge = lastDisScaleMeasureResult.bodyAge;
        float weightControl = lastDisScaleMeasureResult.weightControl;
        String measureTime = lastDisScaleMeasureResult.measureTime;

        boolean isSave;
        if (CMR == null) {
            //说明记录当前的那个表还没有记录这个值
            CMR = new SeneoUserCurrentMeasureRecord();
            CMR.host_id = currentUserinfos.host_id;
            CMR.user_id = currentUserinfos.user_id;
            isSave = true;
        } else {
            isSave = false;
        }
        CMR.weight = weigh;
        CMR.fat = fat;
        CMR.bmi = bmi;
        CMR.bmr = bmr;
        CMR.visceralFat = vf;
        CMR.muscleVolume = muscle;
        CMR.boneVolume = bone;
        CMR.protein = protein;
        CMR.waterRate = water;
        CMR.skeletalMuscle = skeletal;
        CMR.bodyScore = bodyScore;
        CMR.bodyAge = bodyAge;
        CMR.measureTime = scaleMeasureResult.measureTime;
        CMR.weightControl = weightControl;

        //01更新用户数据记录表
        if (isSave) {
            CMR.save();
        } else {
            CMR.update();
        }


        GoogleFitManager googleFitManager = GoogleFitManager.getInstance();
        boolean isOpen = SeneoUtils.getGoogleFitOpenFlag();
        GoogleSignInAccount googleSignInAccount = googleFitManager.getLastSignedInAccount(CommonLib.APPContext);
        if (isOpen && googleSignInAccount != null) {
            long time = System.currentTimeMillis();
            long time2 = time + 1000;

            googleFitManager.insertWeight(CommonLib.APPContext, weigh, time, time2, new GoogleFitManager.InsertDataCallback() {
                @Override
                public void success() {
                    LogUtils.i(TAG, "googleFit插入体重成功");
                }

                @Override
                public void failure(int error, String desc) {
                    LogUtils.i(TAG, "googleFit插入体重失败");
                }
            });

            if (bmr > 0) {
                googleFitManager.insertBmr(CommonLib.APPContext, bmr, time, time2, new GoogleFitManager.InsertDataCallback() {
                    @Override
                    public void success() {
                        LogUtils.i(TAG, "googleFit插入Bmr成功");
                    }

                    @Override
                    public void failure(int error, String desc) {
                        LogUtils.i(TAG, "googleFit插入Bmr失败");
                    }
                });
            }


            if (fat > 0) {
                googleFitManager.insertFat(CommonLib.APPContext, (fat), time, time2, new GoogleFitManager.InsertDataCallback() {
                    @Override
                    public void success() {
                        LogUtils.i(TAG, "googleFit插入Fat成功");
                    }

                    @Override
                    public void failure(int error, String desc) {
                        LogUtils.i(TAG, "googleFit插入Fat失败");
                    }
                });
            }





            SeneoUtils.uploadWeight2Fitbit(NumUtils.numBaoLiuWei1(weigh),NumUtils.numBaoLiuWei1(fat),measureTime);

        }










        //02更新当前用户记录表
        //保存当前的体重和阻抗
        //记录用户信息表
        currentUserinfos.currentWeight = weigh;
        currentUserinfos.impedance = (int) scaleMeasureResult.resistance;
        currentUserinfos.update();

        //更新实时的体脂用户表
        //体脂秤用户表
        ScaleUser.getUser().setImpedance(scaleMeasureResult.resistance);
        ScaleUser.getUser().setWeight(weigh);

        LogUtils.i2(TAG, "----当前用户信息：" + lastDisSeneoUserinfos);
        LogUtils.i2(TAG, "----当前测量信息：" + lastDisScaleMeasureResult);


        SeneoUtils.updateCurrentInfoDiaplay(TAG);//通知其他页面用户信息已经改变的意思

        displayLastMeasureData();//显示最新的测量的数据
    }


    //显示最新的测量数据 这个方法一般在切换用户的时候会调用 也即是从网络上获取到数据时会调用此方法
    public void displaySeneoUserCurrentMeasureRecordData() {
        LogUtils.i(TAG, "切换用户,显示最新的测量的数据");
        if (getActivity() == null) {
            LogUtils.i(TAG, "切换用户 getActivity() == null 被reture");
            return;
        }
        SeneoBTManager seneoBTManager = ((SeneoMainActivity) getActivity()).getSeneoBTManager();
        if (seneoBTManager == null) {
            LogUtils.i(TAG, "切换用户seneoBTManager == null 被reture");
            return;
        }

        SeneoUserinfos seneoUserinfos = seneoBTManager.getCurrentUserinfos();
        if (seneoUserinfos == null) {
            LogUtils.i(TAG, "切换用户seneoUserinfos == null 被reture");
            return;
        }

        SeneoUserCurrentMeasureRecord currentMeasureRecord = SeneoDbUtils.getSeneoUserCurrentMeasureRecord(seneoUserinfos.host_id, seneoUserinfos.user_id);
        if (currentMeasureRecord == null) {
            LogUtils.i(TAG, "切换用户currentMeasureRecord == null 被reture");
            ViewUtils.displayView(dash_tip_ll);
            ViewUtils.goneView(dash_sv);
            diaplayAllNon();
            return;
        }
        ViewUtils.goneView(dash_tip_ll);
        ViewUtils.displayView(dash_sv);
        int unit = SeneoUtils.getUnitModel();//0kg 1lb
        String unitStr = (unit == 0 ? IUserContant.IDIS.kg : IUserContant.IDIS.lb);
        //设置单位
        ViewUtils.setTextViewStr(dash_weight_unit_tv, unitStr);
        ViewUtils.setTextViewStr(dash_changeWeightValueUnit_tv, unitStr);

        float weigh = currentMeasureRecord.weight;
        float bmi = currentMeasureRecord.bmi;
        float fat = currentMeasureRecord.fat;
        float bmr = currentMeasureRecord.bmr;
        float vf = currentMeasureRecord.visceralFat;
        float muscle = currentMeasureRecord.muscleVolume;
        float bone = currentMeasureRecord.boneVolume;
        float protein = currentMeasureRecord.protein;
        float water = currentMeasureRecord.waterRate;
        float skeletal = currentMeasureRecord.skeletalMuscle;
        float bodyScore = currentMeasureRecord.bodyScore;
        float bodyAge = currentMeasureRecord.bodyAge;
        float weightControl = currentMeasureRecord.weightControl;



        float tofatWeight = weigh;//去脂体重
        if (fat != 0) {
            tofatWeight = (float) (weigh - weigh * fat / 100.0);
        }

        //float weightControl=weigh-BodyCompositionCriteria.getWeightKgByBMI(24,lastDisSeneoUserinfos.height);



        String weightStr = adapterItem(SeneoUtils.adapterDataFloat(weigh, unit));
        String bmiStr = adapterItem(bmi);
        String fatStr = adapterItem2(fat, "%");
        String bmrStr = adapterItem3((int) bmr, "kcal");
        String vfStr = adapterItem(vf);
        String muscleStr = adapterItem2(SeneoUtils.adapterDataFloat(muscle, unit), unitStr);
        String boneStr = adapterItem2(SeneoUtils.adapterDataFloat(bone, unit), unitStr);
        String proteinStr = adapterItem2(protein, "%");
        String waterStr = adapterItem2(water, "%");
        String skeletalStr = adapterItem2(SeneoUtils.adapterDataFloat(skeletal, unit), unitStr);
        String bodyScoreStr = adapterItem0((int) bodyScore);
        String bodyAgeStr = adapterItem3((int) bodyAge, "years old");
        String tofatWeightStr = adapterItem2(SeneoUtils.adapterDataFloat(tofatWeight, unit), unitStr);

        if (fat==0){
            tofatWeightStr=Non;
        }

        String weightControlStr = adapterItem6(SeneoUtils.adapterDataFloat(weightControl, unit), unitStr);
        // String changeWeightStr = adapterItem(SeneoUtils.adapterDataFloat(Math.abs(changeWeightKg),unit));
        //String changeFatStr = adapterItem(Math.abs(changeFat));


        //显示时间

        long measureTimeLong = TimeUtils.parseFormatter1Time(currentMeasureRecord.measureTime).getTime();
        String datetime = TimeUtils.formatDate_nyr(CommonLib.APPContext, measureTimeLong);
        String time = TimeUtils.formatTime20(measureTimeLong);
        ViewUtils.setTextViewStr(dash_measure_time_tv, datetime + " " + time);

        //设置变化的值
        ViewUtils.setTextViewStr(dash_changeWeightValue_tv, Non);
        ViewUtils.setTextViewStr(dash_changefatValue_tv, Non);

        ViewUtils.setImageResource(dash_changeWeight_iv, 0);
        ViewUtils.setImageResource(dash_changefat_iv, 0);


        //设置体重的值
        ViewUtils.setTextViewStr(dash_weight_value_tv, weightStr);
        display12Item(weightStr + unitStr, tofatWeightStr, weightControlStr, bmiStr, fatStr, bmrStr, vfStr, muscleStr, boneStr, proteinStr, waterStr, skeletalStr, bodyScoreStr, bodyAgeStr, Non);


        int height = seneoUserinfos.height;
        //  int roleType = seneoUserinfos.athlete;
        int age = seneoUserinfos.getAge();
        int sex = seneoUserinfos.sex;


        //设置等级标签
        int bmiLevel = BodyCompositionCriteria.bodyQualityStandard1(bmi);
        SeneoUtils.setLabelLevel2(dash_weight_biaozhun_tv, bmi != 0 ? bmiLevel : -1);

        SeneoUtils.setLabelLevel(dash_item_weight2_tv, bmi != 0 ? bmiLevel : -1);

        int tofatLevel = BodyCompositionCriteria.getTofatWeightLevel(age,sex,height,tofatWeight);

        SeneoUtils.setLabelLevel(dash_item_ToFatWeight2_tv, fat != 0 ? tofatLevel : -1);
        SeneoUtils.setLabelLevel(dash_item_WeightControl2_tv, bmi != 0 ? bmiLevel : -1);

        SeneoUtils.setLabelLevel(dash_item_Bmi2_tv, bmi != 0 ? bmiLevel : -1);

        int fatLevel = BodyCompositionCriteria.getFatPercentStand(fat, age, sex);//0偏瘦 1标准- 2标准+ 3轻度肥胖 4重度肥胖
        if (fatLevel == 1 || fatLevel == 2) {
            fatLevel = 1;
        } else if (fatLevel == 3) {
            fatLevel = 2;
        } else if (fatLevel == 4) {
            fatLevel = 3;
        }
        SeneoUtils.setLabelLevel41(dash_item_bodyfatPercentage2_tv, fat != 0 ? fatLevel : -1);


        int bmrLevel = BodyCompositionCriteria.getBMRStand((int) bmr, age, sex);
        SeneoUtils.setLabelLevel(dash_item_Bmr2_tv, bmr != 0 ? bmrLevel : -1);


        int vfLevel = BodyCompositionCriteria.getVisFatStand(vf);
        SeneoUtils.setLabelLevel3(dash_item_visfat2_tv, vf != 0 ? vfLevel : -1);

        int muscleLevel = BodyCompositionCriteria.getMuscleStand(muscle, height, sex);
        SeneoUtils.setLabelLevel(dash_item_muscle2_tv, muscle != 0 ? muscleLevel : -1);//肌肉
        SeneoUtils.setLabelLevel(dash_item_skeletal2_tv, -1);//骨骼肌


        int boneLevel = BodyCompositionCriteria.getBoneStand(bone, weigh, sex);
        SeneoUtils.setLabelLevel(dash_item_bone2_tv, bone != 0 ? boneLevel : -1);

        int proteinLevel = BodyCompositionCriteria.getProteinStand(protein);
        SeneoUtils.setLabelLevel(dash_item_protein2_tv, protein != 0 ? proteinLevel : -1);

        int waterLevel = BodyCompositionCriteria.getWaterStand(water, sex);
        SeneoUtils.setLabelLevel51(dash_item_water2_tv, water != 0 ? waterLevel : -1);


        SeneoUtils.setLabelLevelRes(dash_item_weight_iv, bmi != 0 ? bmiLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_ToFatWeight_iv, fat != 0 ? tofatLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_Bmi_iv, bmi != 0 ? bmiLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_bodyfatPercentage_iv, fat != 0 ? fatLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_Bmr_iv, bmr != 0 ? bmrLevel : -1);
        SeneoUtils.setLabelLevelRes3(dash_item_visfat_iv, vf != 0 ? vfLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_muscle_iv, muscle != 0 ? muscleLevel : -1);//肌肉
        SeneoUtils.setLabelLevelRes(dash_item_bone_iv, bone != 0 ? boneLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_protein_iv, protein != 0 ? proteinLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_water_iv, water != 0 ? waterLevel : -1);

        //设置体型
        if (fat != 0 && muscle != 0) {
            Object[] objects = BodyCompositionCriteria.getSize(fat, age, sex, muscle, height);
            ViewUtils.setTextViewStr(dash_item_size_tv, (String) objects[1]);
        } else {
            ViewUtils.setTextViewStr(dash_item_size_tv, Non);
        }


    }

    private void displayLastMeasureData() {

        ViewUtils.goneView(dash_tip_ll);
        ViewUtils.displayView(dash_sv);


        int unit = SeneoUtils.getUnitModel();//0kg 1lb
        String unitStr = (unit == 0 ? IUserContant.IDIS.kg : IUserContant.IDIS.lb);
        //设置单位
        ViewUtils.setTextViewStr(dash_weight_unit_tv, unitStr);
        ViewUtils.setTextViewStr(dash_changeWeightValueUnit_tv, unitStr);

        if (lastDisScaleMeasureResult == null || lastDisSeneoUserinfos == null) {
            diaplayAllNon();
            return;
        }

        float weigh = lastDisScaleMeasureResult.weight;
        float bmi = lastDisScaleMeasureResult.bmi;
        float fat = lastDisScaleMeasureResult.fat;
        float bmr = lastDisScaleMeasureResult.bmr;
        float vf = lastDisScaleMeasureResult.visceralFat;
        float muscle = lastDisScaleMeasureResult.muscleVolume;
        float bone = lastDisScaleMeasureResult.boneVolume;
        float protein = lastDisScaleMeasureResult.protein;
        float water = lastDisScaleMeasureResult.waterRate;
        float skeletal = lastDisScaleMeasureResult.skeletalMuscle;
        float bodyScore = lastDisScaleMeasureResult.bodyScore;
        float bodyAge = lastDisScaleMeasureResult.bodyAge;


        float tofatWeight = weigh;//去脂体重
        if (fat != 0) {
            tofatWeight = (float) (weigh - weigh * fat / 100.0);
        }
        //float weightControl=weigh-BodyCompositionCriteria.getWeightKgByBMI(24,lastDisSeneoUserinfos.height);
        float weightControl = lastDisScaleMeasureResult.weightControl;


        String weightStr = adapterItem(SeneoUtils.adapterDataFloat(weigh, unit));
        String bmiStr = adapterItem(bmi);
        String fatStr = adapterItem2(fat, "%");
        String bmrStr = adapterItem3((int) bmr, "kcal");
        String vfStr = adapterItem(vf);
        String muscleStr = adapterItem2(SeneoUtils.adapterDataFloat(muscle, unit), unitStr);
        String boneStr = adapterItem2(SeneoUtils.adapterDataFloat(bone, unit), unitStr);
        String proteinStr = adapterItem2(protein, "%");
        String waterStr = adapterItem2(water, "%");
        String skeletalStr = adapterItem2(SeneoUtils.adapterDataFloat(skeletal, unit), unitStr);
        String bodyScoreStr = adapterItem0((int) bodyScore);
        String bodyAgeStr = adapterItem3((int) bodyAge, "years old");

//        String changeWeightStr = adapterItem(SeneoUtils.adapterDataFloat(Math.abs(changeWeightKg), unit));
//        String changeFatStr = adapterItem(Math.abs(changeFat));

        String changeWeightStr = adapterItem5(SeneoUtils.adapterDataFloat(Math.abs(changeWeightKg), unit));
        String changeFatStr = adapterItem5(Math.abs(changeFat));

        String tofatWeightStr = adapterItem2(SeneoUtils.adapterDataFloat(tofatWeight, unit), unitStr);

        if (fat==0){
            tofatWeightStr=Non;
        }

        String weightControlStr = adapterItem6(SeneoUtils.adapterDataFloat(weightControl, unit), unitStr);


        //显示时间
        long measureTimeLong = TimeUtils.parseFormatter1Time(lastDisScaleMeasureResult.measureTime).getTime();
        String datetime = TimeUtils.formatDate_nyr(CommonLib.APPContext, measureTimeLong);
        String time = TimeUtils.formatTime20(measureTimeLong);
        ViewUtils.setTextViewStr(dash_measure_time_tv, datetime + " " + time);


        //设置变化的值


        if (changeWeightKg > 0) {
            ViewUtils.setImageResource(dash_changeWeight_iv, R.drawable.ic_arrow_up);
        } else if (changeWeightKg == 0) {
            ViewUtils.setImageResource(dash_changeWeight_iv, 0);
        } else {
            ViewUtils.setImageResource(dash_changeWeight_iv, R.drawable.ic_arrow_down);
        }
        if (changeFat > 0) {
            ViewUtils.setImageResource(dash_changefat_iv, R.drawable.ic_arrow_up);
        } else if (changeFat == 0) {
            ViewUtils.setImageResource(dash_changefat_iv, 0);
        } else {
            ViewUtils.setImageResource(dash_changefat_iv, R.drawable.ic_arrow_down);
        }

        if (weigh > 0) {
            ViewUtils.setTextViewStr(dash_changeWeightValue_tv, changeWeightStr);
        } else {
            ViewUtils.setTextViewStr(dash_changeWeightValue_tv, Non);
            ViewUtils.setImageResource(dash_changeWeight_iv, 0);
        }

        if (fat > 0) {
            ViewUtils.setTextViewStr(dash_changefatValue_tv, changeFatStr);
        } else {
            ViewUtils.setTextViewStr(dash_changefatValue_tv, Non);
            ViewUtils.setImageResource(dash_changefat_iv, 0);
        }


        //设置体重的值
        ViewUtils.setTextViewStr(dash_weight_value_tv, weightStr);
        display12Item(weightStr + unitStr, tofatWeightStr, weightControlStr, bmiStr, fatStr, bmrStr, vfStr, muscleStr, boneStr, proteinStr, waterStr, skeletalStr, bodyScoreStr, bodyAgeStr, Non);


        int height = lastDisSeneoUserinfos.height;
        //int roleType = lastDisSeneoUserinfos.athlete;
        int age = lastDisSeneoUserinfos.getAge();
        int sex = lastDisSeneoUserinfos.sex;


        //设置等级标签
        int bmiLevel = BodyCompositionCriteria.bodyQualityStandard1(bmi);
        SeneoUtils.setLabelLevel2(dash_weight_biaozhun_tv, bmi != 0 ? bmiLevel : -1);

        int tofatLevel = BodyCompositionCriteria.getTofatWeightLevel(age,sex,height,tofatWeight);

        SeneoUtils.setLabelLevel(dash_item_weight2_tv, bmi != 0 ? bmiLevel : -1);
        SeneoUtils.setLabelLevel(dash_item_ToFatWeight2_tv, fat != 0 ? tofatLevel : -1);
        SeneoUtils.setLabelLevel(dash_item_WeightControl2_tv, bmi != 0 ? bmiLevel : -1);


        SeneoUtils.setLabelLevel(dash_item_Bmi2_tv, bmi != 0 ? bmiLevel : -1);


        int fatLevel = BodyCompositionCriteria.getFatPercentStand(fat, age, sex);//0偏瘦 1标准- 2标准+ 3轻度肥胖 4重度肥胖
        if (fatLevel == 1 || fatLevel == 2) {
            fatLevel = 1;
        } else if (fatLevel == 3) {
            fatLevel = 2;
        } else if (fatLevel == 4) {
            fatLevel = 3;
        }
        SeneoUtils.setLabelLevel41(dash_item_bodyfatPercentage2_tv, fat != 0 ? fatLevel : -1);


        //SeneoUtils.setLabelLevel41(dash_item_bodyfatPercentage2_tv, 2);


        int bmrLevel = BodyCompositionCriteria.getBMRStand((int) bmr, age, sex);
        SeneoUtils.setLabelLevel(dash_item_Bmr2_tv, bmr != 0 ? bmrLevel : -1);


        int vfLevel = BodyCompositionCriteria.getVisFatStand(vf);
        SeneoUtils.setLabelLevel3(dash_item_visfat2_tv, vf != 0 ? vfLevel : -1);


        int muscleLevel = BodyCompositionCriteria.getMuscleStand(muscle, height, sex);
        SeneoUtils.setLabelLevel(dash_item_muscle2_tv, muscle != 0 ? muscleLevel : -1);//肌肉


        SeneoUtils.setLabelLevel(dash_item_skeletal2_tv, -1);//骨骼肌


        int boneLevel = BodyCompositionCriteria.getBoneStand(bone, weigh, sex);
        SeneoUtils.setLabelLevel(dash_item_bone2_tv, bone != 0 ? boneLevel : -1);


        int proteinLevel = BodyCompositionCriteria.getProteinStand(protein);
        SeneoUtils.setLabelLevel(dash_item_protein2_tv, protein != 0 ? proteinLevel : -1);


        int waterLevel = BodyCompositionCriteria.getWaterStand(water, sex);
        SeneoUtils.setLabelLevel51(dash_item_water2_tv, water != 0 ? waterLevel : -1);

        SeneoUtils.setLabelLevelRes(dash_item_weight_iv, bmi != 0 ? bmiLevel : -1);


        SeneoUtils.setLabelLevelRes(dash_item_ToFatWeight_iv, fat != 0 ? tofatLevel : -1);

        SeneoUtils.setLabelLevelRes(dash_item_Bmi_iv, bmi != 0 ? bmiLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_bodyfatPercentage_iv, fat != 0 ? fatLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_Bmr_iv, bmr != 0 ? bmrLevel : -1);
        SeneoUtils.setLabelLevelRes3(dash_item_visfat_iv, vf != 0 ? vfLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_muscle_iv, muscle != 0 ? muscleLevel : -1);//肌肉
        SeneoUtils.setLabelLevelRes(dash_item_bone_iv, bone != 0 ? boneLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_protein_iv, protein != 0 ? proteinLevel : -1);
        SeneoUtils.setLabelLevelRes(dash_item_water_iv, water != 0 ? waterLevel : -1);

        //设置体型
        String sizess = "体型数据:fat" + fat + "--age=" + age + "--sex=" + sex + "--muscle=" + muscle + "--height" + height + "---结果：";
        if (fat != 0 && muscle != 0) {
            Object[] objects = BodyCompositionCriteria.getSize(fat, age, sex, muscle, height);
            ViewUtils.setTextViewStr(dash_item_size_tv, (String) objects[1]);
            sizess += (String) objects[1];
        } else {
            ViewUtils.setTextViewStr(dash_item_size_tv, Non);
            sizess += Non;
        }
        //  ToastUtil.show(sizess);
        LogUtils.i2(TAG, "------" + sizess);


    }


    private void diaplayAllNon() {
        display12Item(Non, Non, Non, Non, Non, Non, Non, Non, Non, Non, Non, Non, Non, Non, Non);
        ViewUtils.setTextViewStr(dash_weight_value_tv, Non);
        ViewUtils.setTextViewStr(dash_measure_time_tv, "");
        int level = -1;
        SeneoUtils.setLabelLevel2(dash_weight_biaozhun_tv, level);
        SeneoUtils.setLabelLevel(dash_item_weight2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_ToFatWeight2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_WeightControl2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_Bmi2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_bodyfatPercentage2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_Bmr2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_visfat2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_muscle2_tv, level);//肌肉
        SeneoUtils.setLabelLevel(dash_item_skeletal2_tv, level);//骨骼肌
        SeneoUtils.setLabelLevel(dash_item_bone2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_protein2_tv, level);
        SeneoUtils.setLabelLevel(dash_item_water2_tv, level);


        ViewUtils.setImageResource(dash_item_weight_iv, 0);
        ViewUtils.setImageResource(dash_item_ToFatWeight_iv, 0);
        ViewUtils.setImageResource(dash_item_WeightControl_iv, 0);
        ViewUtils.setImageResource(dash_item_Bmi_iv, 0);
        ViewUtils.setImageResource(dash_item_bodyfatPercentage_iv, 0);
        ViewUtils.setImageResource(dash_item_Bmr_iv, 0);
        ViewUtils.setImageResource(dash_item_visfat_iv, 0);
        ViewUtils.setImageResource(dash_item_muscle_iv, 0);
        ViewUtils.setImageResource(dash_item_bone_iv, 0);
        ViewUtils.setImageResource(dash_item_protein_iv, 0);
        ViewUtils.setImageResource(dash_item_water_iv, 0);
        ViewUtils.setImageResource(dash_item_skeletal_iv, 0);
        ViewUtils.setImageResource(dash_item_bodyscore_iv, 0);
        ViewUtils.setImageResource(dash_item_bodyage_iv, 0);
        ViewUtils.setImageResource(dash_item_size_iv, 0);


        ViewUtils.setImageResource(dash_changeWeight_iv, 0);
        ViewUtils.setTextViewStr(dash_changeWeightValue_tv, Non);
        ViewUtils.setImageResource(dash_changefat_iv, 0);
        ViewUtils.setTextViewStr(dash_changefatValue_tv, Non);
    }


    private String adapterItem(float value) {
        value = NumUtils.numBaoLiuWei1(value);
        if (value == 0) {
            return Non;
        }
        return value + "";
    }

    private String adapterItem0(int value) {
        if (value == 0) {
            return Non;
        }
        return value + "";
    }

    private String adapterItem2(float value, String unit) {
        value = NumUtils.numBaoLiuWei1(value);
        if (value == 0) {
            return Non;
        }
        return value + unit;
    }

    private String adapterItem3(float value, String unit) {
        int value2 = NumUtils.numBaoLiuWei0(value);
        if (value2 == 0) {
            return Non;
        }
        return value2 + unit;
    }

    private String adapterItem5(float value) {
        float value2 = NumUtils.numBaoLiuWei1(value);
        return value2 + "";
    }

    public static String adapterItem6(float value, String unit) {
        value = NumUtils.numBaoLiuWei1(value);
        if (value == 0) {
            return Non;
        }
        String zhf="";
        if (value>0){
            zhf="+";
        }
        return zhf+value + unit;
    }


    /**
     * @param weight        体重
     * @param toFatWeight   去脂体重
     * @param weightControl 体重控制量
     * @param bmi           bmi指数
     * @param fat           脂肪率
     * @param bmr           bmr指数
     * @param vf            内脂等级
     * @param muscleMass    肌肉量
     * @param bone          骨量
     * @param protein       蛋白质
     * @param water         水分
     * @param skeletal      骨骼肌
     * @param bodySore      身体得分
     * @param bodyAge       身体年龄
     * @param size          体型
     */
    private void display12Item(String weight, String toFatWeight, String weightControl,
                               String bmi, String fat, String bmr, String vf, String muscleMass,
                               String bone, String protein, String water, String skeletal,
                               String bodySore, String bodyAge, String size) {

        ViewUtils.setTextViewStr(dash_item_weight_tv, weight);
        ViewUtils.setTextViewStr(dash_item_ToFatWeight_tv, toFatWeight);
        ViewUtils.setTextViewStr(dash_item_WeightControl_tv, weightControl);
        ViewUtils.setTextViewStr(dash_item_Bmi_tv, bmi);
        ViewUtils.setTextViewStr(dash_item_bodyfatPercentage_tv, fat);
        ViewUtils.setTextViewStr(dash_item_Bmr_tv, bmr);
        ViewUtils.setTextViewStr(dash_item_visfat_tv, vf);
        ViewUtils.setTextViewStr(dash_item_muscle_tv, muscleMass);
        ViewUtils.setTextViewStr(dash_item_bone_tv, bone);
        ViewUtils.setTextViewStr(dash_item_protein_tv, protein);
        ViewUtils.setTextViewStr(dash_item_water_tv, water);
        ViewUtils.setTextViewStr(dash_item_skeletal_tv, skeletal);
        ViewUtils.setTextViewStr(dash_item_bodyscore_tv, bodySore);
        ViewUtils.setTextViewStr(dash_item_bodyage_tv, bodyAge);
        ViewUtils.setTextViewStr(dash_item_size_tv, size);
    }

    //接收到历史数据
    public void onReceiveHistoryRecord(ScaleMeasureResult scaleHisMeasureResult) {
        if (scaleHisMeasureResult == null || mUploadData2ServiceUtil == null || scaleHisMeasureResult.getWeight() <= 0) {
            return;//体重都等于0的数据就摒弃掉吧
        }
        if ((getActivity()) != null) {
            SeneoBTManager seneoBTManager = ((SeneoMainActivity) getActivity()).getSeneoBTManager();
            if (seneoBTManager != null) {
                SeneoUserinfos currentUserinfos = seneoBTManager.getCurrentUserinfos();
                if (currentUserinfos != null) {
                    dealOnReceiveOffMeasureResult(currentUserinfos, scaleHisMeasureResult);
                } else {
                    LogUtils.i(TAG, "---onReceiveHistoryRecord-找不到用户信息-A");
                }
            }
        }

    }

    private void dealOnReceiveOffMeasureResult(final SeneoUserinfos currentUserinfos, final ScaleMeasureResult scaleHisMeasureResult) {


       // if (scaleHisMeasureResult.isSuspectedData) {
            //是疑义数据

        //只要是离线数据都要弹框提示
            OfflineTextTipDialog textTipDialog = new OfflineTextTipDialog(getActivity(), "Offline data waiting for confirmation");
            textTipDialog.setListener(new OfflineTextTipDialog.ICancelOkBtListener() {
                @Override
                public void cancel(Dialog dialogView) {
                    dialogView.cancel();
                }

                @Override
                public void ok(Dialog dialogView) {
                    dialogView.cancel();
//                    if (mUploadData2ServiceUtil != null) {
//                        mUploadData2ServiceUtil.uploadData(TAG, currentUserinfos, scaleHisMeasureResult);
//                    }

                    SeneoUserCurrentMeasureRecord cm = SeneoDbUtils.getSeneoUserCurrentMeasureRecord(currentUserinfos.host_id,currentUserinfos.user_id);
                    if (cm!=null){
                        long cmT=TimeUtils.parseFormatter1Time(cm.measureTime).getTime();
                        long hisT=TimeUtils.parseFormatter1Time(scaleHisMeasureResult.measureTime).getTime();
                        if (hisT>cmT){
                            dealOnReceiveMeasureResult(currentUserinfos,scaleHisMeasureResult);
                        }
                    }else {
                        dealOnReceiveMeasureResult(currentUserinfos,scaleHisMeasureResult);
                    }


                }
            });
            String valueTip = StringUtils.getResStr(R.string.offline_data_value_tip);
            int unit = SeneoUtils.getUnitModel();
            String weight;
            if (unit == 0) {
                weight = NumUtils.numBaoLiuWei1(scaleHisMeasureResult.weight) + " kg";
            } else {
                weight = NumUtils.numBaoLiuWei1(SeneoUtils.kg2lb(scaleHisMeasureResult.weight)) + " lb";
            }
            String time = SeneoUtils.timeFatmat(scaleHisMeasureResult.getMeasureTime(), 3);
            textTipDialog.show();
            textTipDialog.setTip2Str(String.format(valueTip, weight, time));

//        } else {
//            mUploadData2ServiceUtil.uploadData(TAG, currentUserinfos, scaleHisMeasureResult);
//        }

    }


    public void onReceivedynamicMeasureResult(ScaleMeasureResult result) {
        int unit = SeneoUtils.getUnitModel();
        if (IUserContant.IDIS.kg.equals(result.weightUnit) && unit != 0) {
            SeneoUtils.setUnitModel(0);//kg模式下
            SeneoUtils.sendCommonEventBus(TAG, Set_Fragment.TAG + Dash_Fragment.TAG + Trend_Fragment.TAG, IUserContant.ICMDS.UPDATE_UNIT_DISPLAY, "更新单位显示");
        } else if (IUserContant.IDIS.lb.equals(result.weightUnit) && unit != 1) {
            SeneoUtils.setUnitModel(1);
            SeneoUtils.sendCommonEventBus(TAG, Set_Fragment.TAG + Dash_Fragment.TAG + Trend_Fragment.TAG, IUserContant.ICMDS.UPDATE_UNIT_DISPLAY, "更新单位显示");
        }

        String unitStr = (unit == 0 ? IUserContant.IDIS.kg : IUserContant.IDIS.lb);
        //设置单位
        ViewUtils.setTextViewStr(dash_weight_unit_tv, unitStr);
        ViewUtils.setTextViewStr(dash_changeWeightValueUnit_tv, unitStr);

        String weightStr = adapterItem5(SeneoUtils.adapterDataFloat(result.weight, unit));
        //设置体重的值
        ViewUtils.setTextViewStr(dash_weight_value_tv, weightStr);

    }

    public void setDisplayNullMeasure() {
        ViewUtils.goneView(dash_sv);
        ViewUtils.displayView(dash_tip_ll);
        diaplayAllNon();
    }
}
